﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using Glass;
namespace kiosk
{

    // public 
    
    public partial class DisplayButtons : Form
    {

        //private List<Button> btnGroup;
        //
        private List<GlassButton> gGBtn;
        //
        private GlassButton LastDGpresentBtn = null;
        private Int32 GroupID = -1;
        private DataBaseHandler dHandler;
        private Int32 mcRowID;
        private DataTable dt;
      
        private List<Int32> m_PreGroupCollection;
        private  int MAXROWDISPLAY;
        private Int32 mDispayedRow;
        // Conveter string
        private TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));

        private TaskbarHide.FormState fm;
       
       
        // old Constractor
        public DisplayButtons( Int32 gid,  DataBaseHandler handler)
        {
            
            InitializeComponent();
            dHandler = handler;
            this.GroupID = gid;
            // testing
            this.MAXROWDISPLAY = ApplicationCache.bNoButton;
            
            this.createDataTable();

            //

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            m_PreGroupCollection = new List<int>();

            this.homeBtn.Size = new System.Drawing.Size(ApplicationCache.bWidth, 52);
            this.homeBtn.Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin+20, ApplicationCache.bButtomMargin);

            mDispayedRow = 0;
            mcRowID = 0;
           // buttonAddTimer.Enabled = true;
            
            

        }


        // New Constractor
        public DisplayButtons(DataBaseHandler handler)
        {

           
            InitializeComponent();
            dHandler = handler;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.MAXROWDISPLAY = ApplicationCache.bNoButton;
            mDispayedRow = 0;
            mcRowID = 0;
            
             m_PreGroupCollection = new List<int>();

             this.homeBtn.Size = new System.Drawing.Size(ApplicationCache.bWidth, 52);
             this.homeBtn.Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin+20, ApplicationCache.bButtomMargin);


            /*
            buttonAddTimer.Enabled = true;
            this.m_PreGroupCollection = new List<int>();
            */

        }











        // code that overrides the ctrl+N key to provide the editing falicility.

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            
            if (keyData == Keys.F3)
            {
                this.TopMost = false;
                GBTextInput gbtext = new GBTextInput(this.GroupID);
                gbtext.ShowDialog();
                // Refreshing code of the system
                this.createDataTable();
                btnFlowLayout.Controls.Clear();
                btnFlowLayout.Refresh();
                this.mcRowID = 0;
                this.mDispayedRow = 0;
                if (gGBtn != null)
                    gGBtn.Clear();
                this.buttonAddTimer.Enabled = true;
                this.TopMost = true;

            }
            if (keyData == Keys.F4)
            {
                this.TopMost = false;
                EditButtons editbtn = new EditButtons(this.GroupID);
                editbtn.ShowDialog();
                // Refresh code of the system
                if (this.LastDGpresentBtn != null)
                {
                Int32 itemid = Convert.ToInt32(this.LastDGpresentBtn.Tag);
                DataTable dt2 = this.dHandler.getAllGroupItems(GroupID);
                if(dt2.Rows.Count == 0)
                {
                    this.dHandler.setDisplayGroupTONull(itemid);
                    // Must be present in the 
                    this.dHandler.delGroup(this.GroupID);
                    
                    
                   //this.Close();
                    return base.ProcessCmdKey(ref msg, keyData);

                }
                
                }
               
                DataTable dt3 = this.dHandler.getAllGroupItems(this.GroupID);
                if(dt3.Rows.Count == 0)
                {
                    this.dHandler.delGroup(this.GroupID);

                
                }

                
                this.createDataTable();
                btnFlowLayout.Controls.Clear();
               // btnFlowLayout.Refresh();
                this.mcRowID = 0;
                this.mDispayedRow = 0;
                if (gGBtn != null)
                    gGBtn.Clear();
                this.buttonAddTimer.Enabled = true;
                this.TopMost = true;

            }

            if (keyData == Keys.F1)
            {
                this.TopMost = false;
                AboutBox1 ab = new AboutBox1();
                ab.ShowDialog();
                this.TopMost = true;

            }
            
            if (keyData == Keys.F2)
            {
                fm.Restore(this);

            }

            if (keyData == Keys.F8)
            {
                GeneralOpt.pictIDChange = -1;
                this.TopMost = false;
                BackGroundImages bg = new BackGroundImages();
                bg.ShowDialog();
                MessageBox.Show("Please wait while for the changes to take place");
                if (GeneralOpt.pictIDChange != -1)
                {
                    ApplicationCache.LoadBgImages();

                    this.Invalidate();
                    this.mainTPanelLayout.Invalidate();
                    this.btnFlowLayout.Invalidate();


                    this.TopMost = true;
                }


                 this.TopMost = true;

            }


            // the mergion window
            if(keyData == Keys.F5)
            {

                this.TopMost = false;
                Margin_Window marginWin = new Margin_Window(2);
                marginWin.ShowDialog();
                
                ApplicationCache.loadMarginsSettings();



                this.homeBtn.Size = new System.Drawing.Size(ApplicationCache.bWidth, 52);
                this.homeBtn.Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin + 20, ApplicationCache.bButtomMargin);
                this.MAXROWDISPLAY = ApplicationCache.bNoButton;
                
                btnFlowLayout.Controls.Clear();
                // btnFlowLayout.Refresh();
                this.mcRowID = 0;
                this.mDispayedRow = 0;
                if (gGBtn != null)
                    gGBtn.Clear();
                this.buttonAddTimer.Enabled = true;
                this.TopMost = true;


            
            
            }

            if(keyData == Keys.F9)
            {

                this.TopMost = false;
                Margin_Window marginWin = new Margin_Window(3);
                marginWin.ShowDialog();

                ApplicationCache.loadMarginsSettings();
               this.groupBackBtn.Size = new Size (ApplicationCache.gWidth, ApplicationCache.gHeight);
                this.groupBackBtn.Padding = new Padding(ApplicationCache.gLeftMargin, ApplicationCache.gTopMargin, ApplicationCache.gRightMargin, ApplicationCache.gButtomMargin);
                this.groupBackBtn.PerformLayout();



                this.TopMost = true;


            
            }







            return base.ProcessCmdKey(ref msg, keyData);
        }

        //----------------------------------------------------------------------------

        #region CreateTable
        private void createDataTable()
         {
            
            
             if (dt != null)
                 dt.Reset();
             try
             {
                 dt = dHandler.getAllGroupItems(this.GroupID);
                
             }
            catch(Exception ex)
             {
                 MessageBox.Show(ex.Message);
            
            }
            // Enable or disable the up and the down keys
            //
               
            //
            
             
        }

        // Create a specific group dataTable

        private void createDataTable(int Gid)
        {


            if (dt != null)
                dt.Reset();
            try
            {
                dt = dHandler.getAllGroupItems(Gid);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            // Enable or disable the up and the down keys
            //

            //


        }

        #endregion


        private void butonAddTimer_Tick(object sender, EventArgs e)
        {
            // Currently adds all the group items in the database

            if (gGBtn == null)
            {
                //btnGroup = new List<Button>();
                gGBtn = new List<GlassButton>();
            }
            int count;
            if (mcRowID < MAXROWDISPLAY && mDispayedRow < dt.Rows.Count)
            {
                gGBtn.Add(new GlassButton());
                count = gGBtn.Count - 1;
                gGBtn[count].Text = (string)dt.Rows[mDispayedRow]["ItemText"];
                gGBtn[count].Tag = dt.Rows[mDispayedRow]["ItemID"];
                gGBtn[count].Size = new System.Drawing.Size(ApplicationCache.bWidth, ApplicationCache.bHeight);
                gGBtn[count].Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin, ApplicationCache.bButtomMargin);
                gGBtn[count].Font = (Font)converter.ConvertFromString((String)dt.Rows[mDispayedRow]["ItemFont"]);
                // btnGroup[count].Location = new System.Drawing.Point(x, y);
                gGBtn[count].Click += new System.EventHandler(this.GroupBtn_Click);
                btnFlowLayout.Controls.Add(gGBtn[count]);
                mDispayedRow += 1;
                mcRowID += 1;
               
                



            }

            else if (mDispayedRow < dt.Rows.Count)
            {

               
                gGBtn.Add(new GlassButton());
                count = gGBtn.Count - 1;
                gGBtn[count].Text = "More";
                gGBtn[count].Tag = -2;
                gGBtn[count].Size = new System.Drawing.Size(ApplicationCache.bWidth, ApplicationCache.bHeight);
                gGBtn[count].Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin, ApplicationCache.bButtomMargin);
                gGBtn[count].Font = new Font("Arial", 14, FontStyle.Regular);
                // Better impletation is needed
                // gGBtn[count].Font = (Font)converter.ConvertFromString((String)dt.Rows[mDispayedRow]["ItemFont"]);
                // btnGroup[count].Location = new System.Drawing.Point(x, y);
                gGBtn[count].Click += new System.EventHandler(this.GroupBtn_Click);
                btnFlowLayout.Controls.Add(gGBtn[count]);
               
                this.buttonAddTimer.Enabled = false;




            }
            
            
                      
            
            else
            {
                this.buttonAddTimer.Enabled = false;
            }

           
         
        }

       
        private void GroupBtn_Click(object sender, EventArgs e)
        {
            this.TopMost = false;
            GlassButton temp = (GlassButton)sender;

            Int32 Gid ,Pgid;
            Int32  itemid = Convert.ToInt32(temp.Tag);
           
            // Check the content depended on the item,mid then decide which window to open
            try
            {
                // logic for the More Button
                Int32 result = dHandler.isItemDataPresent(itemid);

                if(itemid == -2)
                {

                    btnFlowLayout.Controls.Clear();
                    btnFlowLayout.Refresh();


                    
                    gGBtn.Clear();
                    //

                    gGBtn.Add(new GlassButton());
                    Int32 count = gGBtn.Count - 1;
                    gGBtn[count].Text = "--------- Previous ---------";
                    gGBtn[count].Tag = -3;
                    gGBtn[count].Size = new System.Drawing.Size(ApplicationCache.bWidth, ApplicationCache.bHeight);
                    gGBtn[count].Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin, ApplicationCache.bButtomMargin);
                    gGBtn[count].Font = new Font("Arial", 14, FontStyle.Regular);
                    // Better impletation is needed
                    // gGBtn[count].Font = (Font)converter.ConvertFromString((String)dt.Rows[mDispayedRow]["ItemFont"]);
                    // btnGroup[count].Location = new System.Drawing.Point(x, y);
                    gGBtn[count].Click += new System.EventHandler(this.GroupBtn_Click);
                    btnFlowLayout.Controls.Add(gGBtn[count]);
                    btnFlowLayout.Refresh();

                   
                    //
                    
                    mcRowID = 0;
                    buttonAddTimer.Enabled = true;



                
                }

               
                  // Logic if i click the prvious button

                else if(itemid == -3)
                {

                    btnFlowLayout.Controls.Clear();
                    btnFlowLayout.Refresh();
                    mDispayedRow = mDispayedRow - mcRowID - MAXROWDISPLAY;
                    gGBtn.Clear();
                    if(mDispayedRow >0)
                    {

                        //Addthe button again

                        btnFlowLayout.Controls.Clear();
                        btnFlowLayout.Refresh();



                        gGBtn.Clear();
                        //

                        gGBtn.Add(new GlassButton());
                        Int32 count = gGBtn.Count - 1;
                        gGBtn[count].Text = "--------- Previous ---------";
                        gGBtn[count].Tag = -3;
                        gGBtn[count].Size = new System.Drawing.Size(ApplicationCache.bWidth, ApplicationCache.bHeight);
                        gGBtn[count].Margin = new System.Windows.Forms.Padding(ApplicationCache.bLeftMargin, ApplicationCache.bTopMargin, ApplicationCache.bRightMargin, ApplicationCache.bButtomMargin);
                        gGBtn[count].Font = new Font("Arial", 14, FontStyle.Regular);
                        // Better impletation is needed
                        // gGBtn[count].Font = (Font)converter.ConvertFromString((String)dt.Rows[mDispayedRow]["ItemFont"]);
                        // btnGroup[count].Location = new System.Drawing.Point(x, y);
                        gGBtn[count].Click += new System.EventHandler(this.GroupBtn_Click);
                        btnFlowLayout.Controls.Add(gGBtn[count]);


                    
                    
                    }
                   
                    mcRowID = 0;
                    buttonAddTimer.Enabled = true;
                    

                
                }
                
                
                
                
               
                else if (result == -1)
                {
                    
                    Gid = dHandler.getGroupIDOnItemID(itemid);
                    Pgid = dHandler.getParentGIDFGroup(Gid);
                    
                    int dgroup = this.dHandler.getDisplayGroup(itemid);
                  
                    if (Gid != Pgid && dgroup!= -1)
                    { 
                        // parent group is present

                        //check if there is more then 0 item in the group
                        DataTable dt = this.dHandler.getAllGroupItems(dgroup);
                        if (dt.Rows.Count >= 1)
                        {
                            //
                          
                            
                            //this.createDataTable();
                            // Logic is displaygroup is present in the Item

                            this.groupBackBtn.Enabled = true;
                            this.groupBackBtn.Visible = true;
                            this.LastDGpresentBtn = temp;

                            this.groupBackBtn.Text = "Go Back to   " + temp.Text;
                            this.m_PreGroupCollection.Add(this.GroupID);
                            this.GroupID = dgroup;
                            this.createDataTable();
                            btnFlowLayout.Controls.Clear();
                           // btnFlowLayout.Refresh();
                            this.mcRowID = 0;
                            this.mDispayedRow = 0;
                            if (gGBtn != null)
                                gGBtn.Clear();
                            this.buttonAddTimer.Enabled = true;
                        }
                        else
                        { 
                         // clean up
                            // delete group and set the display group = null;
                            this.dHandler.setDisplayGroupTONull(itemid);
                        
                        
                        }

                    }
                    else   // No data present in the item
                    {
                       
                            // show the data input wizard
                            WizardWindow wWindow = new WizardWindow(GroupID, itemid, dHandler);
                            wWindow.ShowDialog();
                            
                    
                    }

                }
               else
                {
                    Int32 dType;
                    dType = dHandler.getDataType(result); 
                    if (dType == (int)DataOption.TextDataType)
                    {
                        // Display the data
                        // Text window
                        //ShowTextData tdata = new ShowTextData(result);
                       
                        PdfViewer viewForm = new PdfViewer(result);
                        viewForm.ShowDialog(); 


                    }
                   
                    else
                    {

                        MessageBox.Show("Unknown option in the item contract admin");
                    }

                }

                this.TopMost = true;
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        
       
        }

        //private void nextBtn_Click(object sender, EventArgs e)
        //{

        //    flowLayoutPanel1.Controls.Clear();
        //    flowLayoutPanel1.Refresh();

           
        //    this.prevBtn.Enabled = true;
        //    gGBtn.Clear();
            
        //    if (mDispayedRow + MAXROWDISPLAY >= dt.Rows.Count)
        //        this.nextBtn.Enabled = false;
        //    mcRowID = 0;
        //    buttonAddTimer.Enabled = true;

        //}

        //private void prevBtn_Click(object sender, EventArgs e)
        //{

        //     flowLayoutPanel1.Controls.Clear();
        //    flowLayoutPanel1.Refresh();
        //    mDispayedRow = mDispayedRow - mcRowID - MAXROWDISPLAY;
           
        //    this.nextBtn.Enabled = true;
        //    gGBtn.Clear();
        //    mcRowID = 0;
        //    buttonAddTimer.Enabled = true;
        //    if (mDispayedRow == 0)
        //        this.prevBtn.Enabled = false;
            
        //}

        private void DisplayButtons_Load(object sender, EventArgs e)
        {

            //fm = new TaskbarHide.FormState();
            //fm.Maximize(this);
            //this.btnWidth = this.btnFlowLayout.Width - 30;

            
        }

        private void homeBtn_Click(object sender, EventArgs e)
        {
            this.TopMost = false;
            this.Close();
 
           

        }

        #region paintJobs

        private void mainTPanelLayout_Paint(object sender, PaintEventArgs e)
        {

            Graphics g = e.Graphics;
            if(ApplicationCache.ButtonWindowImage != null)
            {
                g.DrawImage(ApplicationCache.ButtonWindowImage, new Rectangle(0, 0, this.mainTPanelLayout.Width, this.mainTPanelLayout.Height), 0, 0, this.mainTPanelLayout.Width, this.mainTPanelLayout.Height, GraphicsUnit.Pixel);
            
            
            }

        }



        private void btnFlowLayout_Paint(object sender, PaintEventArgs e)
        {

            Point p = this.btnFlowLayout.PointToScreen(new Point(this.btnFlowLayout.ClientRectangle.X, this.btnFlowLayout.ClientRectangle.Y));
            Rectangle screenRect = new Rectangle(p.X, p.Y, this.btnFlowLayout.ClientRectangle.Width, this.btnFlowLayout.ClientRectangle.Height);

            if (ApplicationCache.ButtonWindowImage != null)
            {
                Graphics g = e.Graphics;
                g.DrawImage(ApplicationCache.ButtonWindowImage,this.btnFlowLayout.ClientRectangle,screenRect.X,screenRect.Y,screenRect.Width,screenRect.Height,GraphicsUnit.Pixel);
            
            
            }





        }

        private void DisplayButtons_Paint(object sender, PaintEventArgs e)
        {

            if (ApplicationCache.ButtonWindowImage != null)
            {
                Graphics g = e.Graphics;
                g.DrawImage(ApplicationCache.ButtonWindowImage, this.ClientRectangle, this.ClientRectangle.X, this.ClientRectangle.Y, this.Width, this.Height, GraphicsUnit.Pixel);


            }



        }




        #endregion

        private void DisplayButtons_Shown(object sender, EventArgs e)
        {



            if (this.fm == null)
                fm = new TaskbarHide.FormState();
            if (this.GroupID != -1)
            {
                fm.Maximize(this);
                buttonAddTimer.Enabled = true;

            }
        
            
            

        }

        public void setButtnData(Int32 gid, DataBaseHandler handler)
        {
            this.GroupID = gid;
           // this.dHandler = handler;
            createDataTable();
            mDispayedRow = 0;
            mcRowID = 0;
            if (this.btnFlowLayout.Controls.Count >1 )
            {
                this.btnFlowLayout.Controls.Clear();


            }

            if (this.gGBtn != null)
            {
                this.gGBtn.Clear();
                this.gGBtn = null;
            }

        
        }

        private void DisplayButtons_Enter(object sender, EventArgs e)
        {

            
            

        }

        private void groupBackBtn_Click(object sender, EventArgs e)
        {

            if (this.m_PreGroupCollection.Count == 0)
            {
                MessageBox.Show(" No more Option !! Use Home button");
                return;
            }
            else
            {


                this.createDataTable(this.m_PreGroupCollection[this.m_PreGroupCollection.Count - 1]);
                this.GroupID = this.m_PreGroupCollection[this.m_PreGroupCollection.Count - 1];
                this.m_PreGroupCollection.RemoveAt(this.m_PreGroupCollection.Count - 1);

                if (this.m_PreGroupCollection.Count == 0)
                {
                    this.groupBackBtn.Visible = false;
                    //this.groupBackBtn.Enabled = false;
                
                }

                this.btnFlowLayout.Controls.Clear();
                this.btnFlowLayout.Refresh();
                this.mcRowID = 0;
                this.mDispayedRow = 0;
                if (gGBtn != null)
                    gGBtn.Clear();
                this.buttonAddTimer.Enabled = true;

            }



        }

      















    }

}

